package com.gamecollection.fish.model.obj;

import com.gamecollection.fish.utils.GameUtils;

import java.awt.*;
import java.util.Random;

/**
 * 敌方鱼类
 */
public class Enamy extends GameObj {
    
    // 速度
    protected int speed;
    // 方向 (1: 向右, -1: 向左)
    protected int dir = 1;
    // 类型
    protected int type;
    // 分值
    protected int count;
    // 等级
    protected int level;
    // 是否为boss
    protected boolean isBoss = false;
    
    public Enamy() {
        // 随机生成敌方鱼类
        Random random = new Random();
        int fishType = random.nextInt(3) + 1;
        
        switch (fishType) {
            case 1:
                initEnamy1();
                break;
            case 2:
                initEnamy2();
                break;
            case 3:
                initEnamy3();
                break;
        }
    }
    
    private void initEnamy1() {
        this.x = -45;
        this.y = (int) (Math.random() * 700 + 100);
        this.width = 45;
        this.height = 69;
        this.speed = 15;
        this.count = 1;
        this.type = 1;
        this.level = 1;
        this.img = GameUtils.enamy1_img;
        
        // 随机方向
        if (Math.random() > 0.5) {
            this.dir = -1;
            this.x = 1400;
            this.img = GameUtils.enamyr_img;
        }
    }
    
    private void initEnamy2() {
        this.x = -100;
        this.y = (int) (Math.random() * 700 + 100);
        this.width = 100;
        this.height = 100;
        this.speed = 5;
        this.count = 2;
        this.type = 2;
        this.level = 2;
        this.img = GameUtils.enamyl_2img;
        
        // 随机方向
        if (Math.random() > 0.5) {
            this.dir = -1;
            this.x = 1400;
            this.img = GameUtils.enamyr_2img;
        }
    }
    
    private void initEnamy3() {
        this.x = -300;
        this.y = (int) (Math.random() * 700 + 100);
        this.width = 300;
        this.height = 150;
        this.speed = 15;
        this.count = 3;
        this.type = 3;
        this.level = 3;
        this.img = GameUtils.enamyl_3img;
        
        // 随机方向
        if (Math.random() > 0.5) {
            this.dir = -1;
            this.x = 1400;
            this.img = GameUtils.enamyr_3img;
        }
    }
    
    @Override
    public void update() {
        // 移动
        x += dir * speed;
        
        // 如果移出屏幕，重置位置
        if (dir == 1 && x > 1440) {
            x = -width;
            y = (int) (Math.random() * 700 + 100);
        } else if (dir == -1 && x < -width) {
            x = 1440;
            y = (int) (Math.random() * 700 + 100);
        }
    }
    
    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img, x, y, width, height, null);
    }
    
    /**
     * 获取碰撞矩形
     */
    public Rectangle getRec() {
        if (type == 3) {
            // 大鱼的特殊碰撞检测
            return new Rectangle(x + 40, y + 30, width - 80, height - 60);
        }
        return new Rectangle(x, y, width, height);
    }
    
    // Getter和Setter方法
    public int getSpeed() {
        return speed;
    }
    
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    
    public int getDir() {
        return dir;
    }
    
    public void setDir(int dir) {
        this.dir = dir;
    }
    
    public int getType() {
        return type;
    }
    
    public void setType(int type) {
        this.type = type;
    }
    
    public int getCount() {
        return count;
    }
    
    public void setCount(int count) {
        this.count = count;
    }
    
    public int getLevel() {
        return level;
    }
    
    public void setLevel(int level) {
        this.level = level;
    }
    
    public boolean isBoss() {
        return isBoss;
    }
    
    public void setBoss(boolean boss) {
        isBoss = boss;
        if (boss) {
            this.x = -1000;
            this.y = (int) (Math.random() * 700 + 100);
            this.width = 340;
            this.height = 340;
            this.speed = 100;
            this.count = 0;
            this.type = 10;
            this.level = 5;
            this.img = GameUtils.bossimg;
        }
    }
} 